package com.lanou3g.demo.service.impl;

import com.lanou3g.demo.controller.params.RoleParams;
import com.lanou3g.demo.entity.RoleInfo;
import com.lanou3g.demo.entity.RoleModule;
import com.lanou3g.demo.mapper.RoleInfoMapper;
import com.lanou3g.demo.service.IRoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleInfoService implements IRoleInfoService {

    @Autowired
    private RoleInfoMapper roleInfoMapper;

    /**
     * 获取角色id
     *
     * @param roleInfo
     * @return
     */
    @Override
    public RoleInfo selectRoleId(RoleInfo roleInfo) {
        RoleInfo ri = roleInfoMapper.selectRole(roleInfo);
        return ri;
    }

    /**
     * 重复角色名禁止插入
     *
     * @return
     */
    @Override
    public boolean selectRole(RoleInfo roleInfo) {
        RoleInfo role = roleInfoMapper.selectRole(roleInfo);
        return role == null;
    }

    /**
     * 角色管理	角色浏览	查看所有角色
     *
     * @return
     */
    @Override
    public List<RoleInfo> findAllWithRole() {
        return roleInfoMapper.findAllWithRole();
    }

    /**
     * 角色管理 增加角色	增加新角色
     *
     * @param roleParams
     * @return
     */
    @Transactional
    @Override
    public int addRoleWithRoleModule(RoleParams roleParams) {

        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setName(roleParams.getrName());
        boolean b = selectRole(roleInfo);
        if (b) {
            roleInfoMapper.insertWithRoleInfo(roleInfo);

            List<RoleModule> roleModules = new ArrayList<>();
            for (Integer roleModuleId : roleParams.getRoleModuleIds()) {
                RoleModule roleModule = new RoleModule();
                roleModule.setRoleId(roleInfo.getRoleId());
                roleModule.setModuleId(roleModuleId);
                roleModules.add(roleModule);
            }
            int i = roleInfoMapper.insertWithRoleModule(roleModules);
            return i;
        } else {
            return -1;
        }
    }

    /**
     * 修改:查询回显
     *
     * @param roleInfo
     * @return
     */
    @Override
    public RoleParams selectRoleModule(RoleInfo roleInfo) {
        RoleInfo role = roleInfoMapper.selectRole(roleInfo);
        RoleInfo ri = new RoleInfo();
        ri.setRoleId(role.getRoleId());
        ri.setName(role.getName());

        List<RoleModule> roleModules = roleInfoMapper.selectRoleModule(ri);

        List<Integer> moduleIds = new ArrayList<>();
        for (RoleModule roleModule : roleModules) {
            moduleIds.add(roleModule.getModuleId());
        }
        RoleParams roleParams = new RoleParams();
        roleParams.setrName(ri.getName());
        roleParams.setRoleModuleIds(moduleIds);
        return roleParams;
    }

    /**
     * 删除角色
     *
     * @param roleInfo
     * @return
     */
    @Transactional
    @Override
    public int deleteRoleWithRoleModule(RoleInfo roleInfo) {
        int j = roleInfoMapper.deleteRole(roleInfo);
        if (j > 0) {
            int i = roleInfoMapper.deleteRoleWithRoleModule(roleInfo);
            return i;
        }
        return -1;
    }

}
